<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ActivityTime_Controlers
 *
 * @author Samuel
 */
defined('BASEPATH') or die('forbidden');

class Modulecopy_Controller extends Controller {

    private $inc;

	private $areaList;	//区名信息
	private $areaStreateList;	//模块策略对应表
    
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $this->inc = $this->config['inc'];

		//区名信息
		if (false == $this->getAreaList()) {
			return;
		}

		//得到模块策略信息
		if (false == $this->getModStrategyList()) {
			return;
		}
    }

	function index() {
		
		if(is_submit())
		{
			return $this->mysub();
		}
		
		$this->view->assign('areastratelist',$this->areaStreateList);
		$this->view->assign('arealist',$this->areaList);
		$this->view->display("activity/modulecopy/index");
	}

	function mysub() {

			//默认是30秒后无法响应，在此修改下时间为无限长
			set_time_limit(0);

			//print_r($_POST);
			
			$receiveData = $_POST['row'];
			
			//print_r($receiveData);
			
			//print_r($data);
			$errorArea = array();	//错误区
			$rightArea = array();	//正确区			
			$insertData = array();	//插入到数据库中的数据
			
			$srcArea = $receiveData['srcarea'];	//源区号，此只有一个数据
			$srcTableEnum = $receiveData['tableenum'];	//源需要修改的活动，此也是个数组
			$desArea = $receiveData['desarea'];	//目标区号，此是个数组
			
			//一个区一个区的发送
			foreach ($desArea as $key => $areanum)
			{
				if($areanum == $srcArea)
				{
					continue;	//自己不发给自己
				}
				//$areanum是区号
				
				//echo " key = $key ";
				//print_r($value);

				$sendJson = array();	//发送给服务器的json数据
				$sendSuccessData = array();	//临时要插入到数据库中的数据
				
				unset($sendJson);
				
				
				$sendJson = array();
				
				$i = 1;
				foreach ($srcTableEnum as $subkey => $tableenum )
				{
					unset($jsonData);
					$jsonData = array();
					//$tableenum 是tableenum
					$jsonData['tableenum'] = (string)$tableenum;
					$jsonData['tablename'] = (string) $this->areaStreateList[$srcArea][$tableenum]['tablename'];
					$jsonData['tabledes'] = (string) $this->areaStreateList[$srcArea][$tableenum]['tabledes'];
					$jsonData['strategyid'] = (string)$this->areaStreateList[$srcArea][$tableenum]['strategyid'];
					$jsonData['des'] = (string)$this->areaStreateList[$srcArea][$tableenum]['des'];
					$jsonData['starttime'] = (string)$this->areaStreateList[$srcArea][$tableenum]['starttime'];
					$jsonData['endtime'] = (string)$this->areaStreateList[$srcArea][$tableenum]['endtime'];
					
					$sendJson[] = $jsonData;
					
					//以下几个是插入数据库需要的字段
					$jsonData['areaid'] = (string)$areanum;
					$jsonData['areaname'] = (string)$this->areaList[$areanum]['name'];
					$jsonData['strategyname'] = (string)$this->areaStreateList[$srcArea][$tableenum]['strategyname'];
					$jsonData['activityid'] = (string)$this->areaStreateList[$srcArea][$tableenum]['activityid'];
					
					$sendSuccessData[$i] = $jsonData;
					++$i;
				}
				//echo " sendjson:";
				//print_r($sendJson);
				//echo " sednsuccessdata: ";
				//print_r($sendSuccessData);
				
				//发送数据
				$url = $this->areaList[$areanum]['host']."action=52&json_str=".json_encode($sendJson);

				//echo " url = $url ";
				$strResultStr = vpost($url);
				if ($strResultStr != '0' || $strResultStr === FALSE) {
					//发送失败。。。。
					$errorArea[] = $areanum;

					//echo " error url = $url";
					//echo " error send area: $value ";
					//去掉此数据
					//unset($sendSuccessData[$i]);
				}else
				{
					//发送成功
					$insertData[$areanum] = $sendSuccessData;	//改送成功就要插入到数据库中去
					//echo " success send area: $key ";
					$rightArea[] = $areanum;
				}
			}

			//echo "errorarea:";
			//print_r($errorArea);
			//echo "rightarea:";
			//print_r($rightArea);
			//echo "insert data:";
			//print_r($insertData);

			if(0 < count($rightArea))
			{
				echo " send success area:";
				foreach ($rightArea as $key => $value)
				{
					echo " $value, ";
				}
			}
			if(0 < count($errorArea))
			{
				echo " send error area:";
				foreach ($errorArea as $key => $value)
				{
					echo " $value, ";
				}
			}
			//只有正确的活动才插入到数据库中去
			foreach ($insertData as $areanum => $value)
			{
				foreach ($value as $subkey => $subvalue)
				{
					//先删除下数据库中的重复数据
					if (false != $this->db->delete("act_area_strategy", array('areaid'=>$subvalue['areaid'],'tableenum'=>$subvalue['tableenum']))) {
						//;
						//echo " delete data error areaid = ".$subvalue['areaid']." tableenum = ".$subvalue['tableenum'];
					}
	
					if(false != $this->db->insert('act_area_strategy', $subvalue,true))
					{
					}else
					{
						//echo " insert error ";
						print_r($value[areaid]);
					}
				}
			}
		
	}

	

	//得到模块策略信息
	function getModStrategyList()
	{
		$sql = "SELECT *  FROM  act_area_strategy";
		$this->db->query($sql);
		//$number = 0;
		while ($n = $this->db->get_array('default'))
		{
			//date('YYYY-mm-dd hh:ii:ss',$n['starttime']);
			//$this->areaStreateList[$n['areaid']]['strategyid'] = $n['strategyid'];
			$this->areaStreateList[$n['areaid']][$n['tableenum']]['areaname'] = $n['areaname'];
			$this->areaStreateList[$n['areaid']][$n['tableenum']]['strategyid'] = $n['strategyid'];
			$this->areaStreateList[$n['areaid']][$n['tableenum']]['strategyname'] = $n['strategyname'];
			$this->areaStreateList[$n['areaid']][$n['tableenum']]['tabledes'] = $n['tabledes'];
			$this->areaStreateList[$n['areaid']][$n['tableenum']]['starttime'] =$n['starttime'];
			$this->areaStreateList[$n['areaid']][$n['tableenum']]['endtime'] =$n['endtime'];
			$this->areaStreateList[$n['areaid']][$n['tableenum']]['tablename'] = $n['tablename'];
			$this->areaStreateList[$n['areaid']][$n['tableenum']]['activityid'] = $n['activityid'];
			$this->areaStreateList[$n['areaid']][$n['tableenum']]['des'] = $n['des'];
		}

		if(0 == count($this->areaStreateList))
		{
			echo "<script>alert( tablename ".$value['tablename']." '" . translate('C_CREATE_TABLENAME') . "');</script>";
			return false;
		}
			
		return true;
	}

	//得到分区信息
	function getAreaList()
	{
		$sql = "SELECT *  FROM {pre}areasample order by id asc";
		$this->db->query($sql);
		//echo "sql:$sql";
		while ($n = $this->db->get_array('default'))
		{
			//print_r($n);
			$this->areaList[$n['id']]['name']=$n['name'];
			$this->areaList[$n['id']]['host']=$n['host'];
		}
		//print_r($this->strategyList);
		if(0 == count($this->areaList))
		{
			echo "<script>alert('" . translate('C_ADD_AREA') . "');</script>";
			return false;
		}
		return true;
	}


	//adajx技术返回给页面
	function getStrategyId()
	{
		$tableenum = $_GET['tableenum'];
		foreach ($this->areaStreateList[$tableenum] as $key => $value)
		{
			echo "<option value=".$key.">".$value."</option>";
		}
	}

}

?>
